
#include <stdint.h>
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "portmacro.h"
#include "sdkconfig.h"
#include "esp_log.h"


#define FINGER_UART_NUM UART_NUM_1
#define PIN_NUM_TX GPIO_NUM_1  //A5
#define PIN_NUM_RX GPIO_NUM_2   //A6
void bsp_finger_init(){
    
     const uart_config_t uart_config = {
        .baud_rate = 57600,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .source_clk = UART_SCLK_DEFAULT,
    };
    // Configure UART parameters
    ESP_ERROR_CHECK(uart_param_config(FINGER_UART_NUM, &uart_config));
    ESP_ERROR_CHECK(uart_set_pin(FINGER_UART_NUM, PIN_NUM_TX, PIN_NUM_RX, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
    ESP_ERROR_CHECK(uart_driver_install(FINGER_UART_NUM, 1024 * 2, 1024 * 2, 0, NULL, 0));

    
    uint16_t HEAD = 0xEF01;
    uint32_t ADDRESS = 0xffffffff;
    uint8_t  CMD = 0x01;
    uint16_t length = 0x0003;
    uint8_t data = 0x0f;
    uint16_t check_sum = 0x0013;

    uint8_t packet[12] = {0};
    packet[0] = HEAD>>8;
    packet[1] = HEAD&0xff;
    packet[2] = (ADDRESS>>24)&0xff;
    packet[3] = (ADDRESS>>16)&0xff;
    packet[4] = (ADDRESS>>8)&0xff;
    packet[5] = ADDRESS&0xff;
    packet[6] = CMD;
    packet[7] = length>>8;
    packet[8] = length&0xff;
    packet[9] = data;
    packet[10] = check_sum>>8;
    packet[11] = check_sum&0xff;


    uart_write_bytes(FINGER_UART_NUM, packet, 12);
    printf("finger send packet len=%d\r\n",12);
    // 读取一秒内的串口数据
    const TickType_t xDelay5Seconds = pdMS_TO_TICKS(500);
    TickType_t start_time = xTaskGetTickCount();

    uint8_t response[50];
    while (xTaskGetTickCount() - start_time < xDelay5Seconds)
     {
        int len = uart_read_bytes(FINGER_UART_NUM, response, 50, xDelay5Seconds);
        if(len >0){
            printf("read len=%d\r\n",len);
            if(len == 28){
                uint16_t recv_head = response[0]<<8|response[1];
                if(recv_head == HEAD){
                    uint32_t recv_address = response[2]<<24|response[3]<<16|response[4]<<8|response[5];
                    uint8_t  packet_id = response[6];
                    uint16_t recv_length = response[7]<<8|response[8];
                    uint8_t  recv_code = response[9];
                    // 接收数据
                    uint8_t recv_sys_info[16];
                    #include <string.h>
                    memcpy(recv_sys_info,response+10,recv_length);
                    // 校验和
                    uint16_t recv_check_sum = response[26]<<8|response[27];

                    // 计算校验和：从包表示开始，到参数结束
                    uint8_t check_sum_data[20];
                    memcpy(check_sum_data,response+6,20);
                    int sum= 0;
                    for(int i=0;i<20;i++){
                        sum += check_sum_data[i];
                    }
                    printf("check sum ok,recv_check_sum=%d,sum=%d\r\n",recv_check_sum,sum);
                    if(sum == recv_check_sum){
                       // printf("check sum ok,recv_check_sum=%d,sum=%d\r\n",recv_check_sum,sum);
                       // 解析系统参数：
                       uint16_t register_id = recv_sys_info[0]<<8|recv_sys_info[1];
                       uint16_t template_size = recv_sys_info[2]<<8|recv_sys_info[3];
                       uint16_t finger_size = recv_sys_info[4]<<24|recv_sys_info[5]<<16;
                       uint16_t score_level = recv_sys_info[6]<<8|recv_sys_info[7];
                       uint32_t device_address = recv_sys_info[8]<<24|recv_sys_info[9]<<16|recv_sys_info[10]<<8|recv_sys_info[11];
                       uint16_t packet_size = recv_sys_info[12]<<8|recv_sys_info[13];
                       uint16_t baud_rate = recv_sys_info[14]<<8|recv_sys_info[15];
                       
                       printf("录入次数=%d,模板大小=%d,指纹库大小=%d,分数等级=%d,设备地址=%lX,包大小=%d,波特率=%d\r\n",
                       register_id,template_size,finger_size,score_level,device_address,packet_size,baud_rate);
                    }
                }
            }    
        }
    }
    printf("exec end\r\n");
    vTaskDelete(NULL);
}

void example_finger_main(){
    xTaskCreate(bsp_finger_init, "example_finger_main", 4096 , NULL, 10, NULL);
}